package com.fengba.nanzhi.tabbar04;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by duan on 2018/4/12.
 */

public class Tabbar04ItemAdpter extends BaseAdapter{

    List<Tabbar04Item> list;
    List<String> section;
    Context context;
    LayoutInflater inflater = null;


    public Tabbar04ItemAdpter(Context context, List<Tabbar04Item>list){
        this.list = list;
        this.context = context;
        // 布局装载器对象
        this.inflater = LayoutInflater.from(context);

    }

    // 适配器中数据集中数据的个数
    @Override
    public int getCount() {
        return list.size();
    }

    // 获取数据集中与指定索引对应的数据项
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    // 获取指定行对应的ID
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View converView, ViewGroup parent){

        View view = null;
        Tabbar04Item item = list.get(position);
        if(converView == null){
            // 当为null的时候，View未被实例化，缓存池中无缓存
            if(item.getName() == ""){
                converView = inflater.inflate(R.layout.tabbar04_item_section, null);
            }else{
                converView = inflater.inflate(R.layout.tabbar04_item, null);
            }
            view = converView;
        }else{
            view = converView;
        }
        ImageView imageView = (ImageView)view.findViewById(R.id.item_img);
        TextView textView = (TextView) view.findViewById(R.id.item_text);
        ImageView next_btn = (ImageView)view.findViewById(R.id.next_btn);
        TextView valueTextView = (TextView)view.findViewById(R.id.value_text);
        imageView.setImageResource(item.getImgId());
        textView.setText(item.getName());
        next_btn.setImageResource(Function.getResourceId("public_0003"));
        if(item.getName().equals("推荐好友")){
            Object user_info = Preferences.getParam(this.context,"user_info","");
            try {
                JSONObject myJson = new JSONObject(user_info.toString());
                //真实姓名
                String realName = myJson.get("gesture_lock").toString().split(",")[2];
                //真实姓名
                String prentAccount = myJson.get("parent_account").toString();

                //隐私
                String privacy = myJson.get("gesture_lock").toString().split(",")[1];
                if(privacy != null && privacy != "" && privacy.equals("0")) {
                    valueTextView.setText(realName+" "+prentAccount);
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return view;
    }

}
